<?php

namespace app\api\model;

use think\Model;

/**
 * token模型
 */
class Token extends Model
{

    protected $name='user_token';

    // 开启自动写入时间戳字段
    protected $auto_timestamp = 'false';

    
    public function setToken($token, $uid, $type) {
 
        $is_exit = $this->where('user_id', $uid)->where('type', $type)->find();
        if ($is_exit) {
            $rst = $this->where('user_id', $uid)->where('type', $type)->update(['token' => $token]);
        } else {
            $rst = $this->save(['user_id' => $uid, 'type' => $type, 'token' => $token]);
        }
   
        return $rst;
     }

      //登录
    public function login_contract_review($cont_data){
        libxml_disable_entity_loader(false);
        $client = new \SoapClient("http://10.108.0.57:8888/ormrpc/services/EASLogin?wsdl");//登录
        $res = $client->login("blqjk","blqjk123","eas","A04","L2",2);
        if(!empty($res->sessionId)){
            $row = $this->newAddContractReview($res->sessionId,$cont_data);
            if($row['msg'] == "success"){
                $wrq = [
                    "code" =>1,
                    'msg'=>"成功",
                    'Serialnum'=>$row['Serialnum']
                ];
            }else{
                $wrq = [
                    "code" =>0,
                    'msg'=>"失败",
                    'Serialnum'=>$row['Serialnum']
                ];
            }
            return $wrq;
        }else{
            $wrq = [
                "code" =>0,
                'msg'=>"登录失败",
                'Serialnum'=>$res
            ];
            return $wrq;
        }
    }

    //合同评审单同步
    public function newAddContractReview($SessionId,$cont_data){
        libxml_disable_entity_loader(false);
        $client = new \SoapClient("http://10.108.0.57:8888/ormrpc/services/WSContractReviewFacade?wsdl");
        //$array = get_object_vars($result);
 
        // echo('<pre>');
        // dump($client->__getFunctions());die;//获取服务器上提供的方法
         //dump($client->__getTypes());die;//获取服务器上提供的数据类型
        // echo('</pre>');
      
         //设置header格式
         $headerVar = new \SoapVar('<ns1><SessionId>'.$SessionId.'</SessionId></ns1>',XSD_ANYXML);
        // $headerVar = new \SoapVar('<ns1><SessionId>4c60ee79-c362-4c9d-bfb8-aa79c53c95a4</SessionId></ns1>',XSD_ANYXML);
 
         //设置header值
         $header = new \SoapHeader('http://10.108.0.57:8888/ormrpc/services/WSContractReviewFacade?wsdl', 'newAddContractReview',$headerVar);
         $client->__setSoapHeaders(array($header));
         
         //请求接口
         $res = $client->newAddContractReview($cont_data);
       //  dump(json_decode($cont_data)->Serialnum);die;
         $ars = [
             'msg'=>$res,
             'Serialnum'=>json_decode($cont_data)->Serialnum
         ];
         return $ars; 
    }
    
     


}
